package com.oig.sys.base.controller;

import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.app.IAppCurrRateService;
import com.oig.sys.base.dto.CurrRateDto;
import com.oig.sys.base.vo.CurrRateVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.YearMonth;
import java.util.List;


@Tag(name = "汇率管理")
@RestController
@RequestMapping("/currRate")
@Slf4j
public class TbCurrRateController {

    private final IAppCurrRateService appCurrRateService;

    @Autowired
    public TbCurrRateController(IAppCurrRateService appCurrRateService){
        this.appCurrRateService = appCurrRateService;
    }

    /**
     * 资源服务加了权限，内部服务调用比较麻烦
     * 我们使用客户端凭证模式处理，客户端凭证的 authorities 配置接口权限
     */
    @PreAuthorize("@pms.hasPermission('/currRate/addCurrRate')")
    @SysLog(value="添加汇率",apiUrl="/currRate/addCurrRate")
    @Operation(summary = "添加汇率，原有汇率会保存到历史表")
    @PostMapping(value = "/addCurrRate")
    public CurrRateVo addCurrRate(@RequestBody @Validated(GroupAdd.class) CurrRateDto currRateDto){
        log.info("add CurrRate {}",currRateDto);
        return this.appCurrRateService.addTbCurrRate(currRateDto);
    }

    @PreAuthorize("@pms.hasPermission('/currRate/updateCurrRate')")
    @SysLog(value="修改汇率",apiUrl="/currRate/updateCurrRate")
    @Operation(summary = "修改汇率")
    @PostMapping(value = "/updateCurrRate")
    public CurrRateVo updateCurrRate(@RequestBody @Validated(GroupEdit.class) CurrRateDto currRateDto){
        log.info("update CurrRate {}",currRateDto);
        return this.appCurrRateService.updateTbCurrRate(currRateDto);
    }

    @PreAuthorize("@pms.hasPermission('/currRate/delCurrRate')")
    @SysLog(value="物理删除汇率",apiUrl="/currRate/delCurrRate")
    @Operation(summary = "物理删除汇率")
    @PostMapping(value = "/delCurrRate/{id}")
    public SysResponse<Integer> delCurrRate(@PathVariable("id") Integer id){
        log.info("del CurrRate id={}", id) ;
        if (id==null||id<=0){
            throw new BadRequestException("参数错误") ;
        }
        this.appCurrRateService.delTbCurrRate(id);
        return SysResponse.ok(id,"物理删除汇率成功");
    }


    @Operation(summary = "根据币种和汇率类型获取汇率")
    @PostMapping(value = "/getCurrRate")
    public CurrRateVo getCurrRate(@RequestParam("currency") String currency, @RequestParam("type") String type){
        log.info("get CurrRate currency={}, type={}", currency, type) ;
        return this.appCurrRateService.getCurrRateByCurr(currency, type);
    }


    @Operation(summary = "抓取汇率,启动爬虫，抓取最新汇率")
    @SysLog(value="抓取实时汇率",apiUrl="/currRate/startSpider")
    @PostMapping(value = "/startSpider")
    public SysResponse<String> startSpider(){
        log.info("start spider...");
        this.appCurrRateService.startRateSpider();
        return SysResponse.ok("开始爬取boc汇率");
    }


    @Operation(summary = "根据抓取的实时汇率，更新上个月第三周星期三海关汇率")
    @SysLog(value = "更新海关汇率", apiUrl = "/currRate/updateCustomsRate")
    @PostMapping(value = "/updateCustomsRate")
    public SysResponse<String> updateCustomsRate() {
        log.info("start update customs rate...");
        this.appCurrRateService.saveCustomsRate();
        return SysResponse.ok("开始爬取boc汇率");
    }

    @Operation(summary = "根据币种和月份获取海关汇率")
    @GetMapping(value = "/listByCustomsCurrAndYearMonth")
    public List<CurrRateVo> listByCustomsCurrAndYearMonth(@RequestParam("currency") List<String> currency,
                                                          @RequestParam("yearMonth") YearMonth yearMonth){
        log.info("listByCustomsCurrAndYearMonth currency={}, yearMonth={}", currency, yearMonth) ;
        if (yearMonth == null) {
            throw new BadRequestException("获取海关汇率月份为空");
        }

        return this.appCurrRateService.listByCustomsCurrAndYearMonth(currency, yearMonth);
    }

}
